import Vue from 'vue'
import VueRouter from 'vue-router'
import {getCookie} from "@/utils/Cookie";


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: () => import('../views/layout/index.vue'),
    children:[
      {
        path:'',
        name:'home',
        meta:{active:1},
        component: () => import('../views/home/index.vue')
      },
      {
        path:'highMember',
        name:'highMember',
        meta:{active:3},
        component: () => import('../views/highQualityMembers/index.vue')
      },
      {
        path:'forum',
        name:'forum',
        meta:{active:2},
        component: () => import('../views/forum/index.vue')
      },
      {
        path:'forum_manage',
        name:'forum_manage',
        meta:{active:4},
        component: () => import('../views/forum_manage/index.vue')
      },

      {
        path:'information',
        name:'information',
        meta:{active:5},
        component: () => import('../views/information/index.vue')
      },
      {
        path:'activity',
        name:'activity',
        meta:{active:6},
        component: () => import('../views/activity/index.vue')
      },
      {
        path:'chat',
        name:'chat',
        meta:{active:1},
        component: () => import('../views/chat/index.vue')
      },
      {
        path:'login',
        name:'login',
        meta:{active:null},
        component: () => import('../views/login/index.vue')
      },
      {
        path:'activityMine',
        name:'activityMine',
        meta:{active:7},
        component: () => import('../views/activityMine/index.vue')
      },
      {
        path:'my_forum',
        name:'my_forum',
        meta:{active:8},
        component: () => import('../views/my_forum/index.vue')
      },
      {
        path:'swearing',
        name:'swearing',
        meta:{active:9},
        component: () => import('../views/swearing/index.vue')
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),//每次都跳转到顶部
  routes
})

router.beforeEach((to, from, next) => {
  // 读取数据
  let token = getCookie('token');
  if(token){
    next();
  }else{
    let reg = RegExp('reg|pc|login|50x')
    if (reg.test(to.path)){
      next()
    } else{
      router.replace('/login').catch(err=>{})
    }
  }
})

export default router
